//
//  ShareCollectionViewController.m
//  B项目
//
//  Created by lanou on 15/12/26.
//  Copyright © 2015年 tzf. All rights reserved.
//

#import "ShareCollectionViewController.h"
#import "ChooseGift.h"
#import "ChooseGiftCollectionViewCell.h"
#import "PopoverView.h"
#import "WebViewController.h"
#import "MJRefresh.h"
@interface ShareCollectionViewController ()
@property (nonatomic,strong) NSMutableArray *modelArray;

@property (nonatomic,strong) NSMutableArray *idArray;



@property (nonatomic,strong) NSArray *personalityArray;
@property (nonatomic,strong) NSArray *targetArray;
@property (nonatomic,strong) NSArray *sceneArray;

@property (nonatomic,assign) NSString *personality1;
@property (nonatomic,assign) NSString *target1;
@property (nonatomic,assign) NSString *scene1;
@end

@implementation ShareCollectionViewController
static NSInteger num = 0;

static NSString * const reuseIdentifier = @"Cell";
- (NSMutableArray *)modelArray{
    if (!_modelArray) {
        _modelArray = [[NSMutableArray alloc]init];
        
    }
    return _modelArray;
}

- (NSMutableArray *)idArray{
    if (!_idArray) {
        _idArray = [[NSMutableArray alloc]init];
    }
    return _idArray;
}

- (void)setData{
    self.target1 =self.target;
    self.personality1 = self.personality;
    self.scene1 = self.scene;

    NSString *string1 = [NSString stringWithFormat:@"&target=%@",self.target1];
    NSString *string2 = [NSString stringWithFormat:@"&personality=%@",self.personality1];
    NSString *string4 = [NSString stringWithFormat:@"&scene=%@",self.scene1];
       if (self.target1 == nil) {
        string1 = @"";
    }
    if (self.personality1 == nil) {
        string2 = @"";
    }
    if (self.scene1 == nil) {
        string4 = @"";
    }
   
    NSString *string = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item_by_type?limit=20&offset=%ld%@%@%@",(long)num,string2,string4,string1];
    NSLog(@"%@",string);
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *rootvc = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSArray *array = rootvc[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            ChooseGift *choose = [[ChooseGift alloc]init];
            [choose setValuesForKeysWithDictionary:dic];
            [self.idArray addObject:choose.ID];
            [self.modelArray addObject:choose];
            
        }
        
        [self.collectionView reloadData];
        
    }];
   
    
    
    
  
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
        __weak ShareCollectionViewController *mainTableView = self;
    [self.collectionView addFooterWithCallback:^{
        num += 20;
        [mainTableView setData];
        [mainTableView.collectionView reloadData];
        
        
        [mainTableView.collectionView footerEndRefreshing];
    }];
    
    __weak ShareCollectionViewController *mainTableView1 = self;
    [self.collectionView addHeaderWithCallback:^{
        _modelArray = [[NSMutableArray alloc]init];
        [mainTableView1 setData];
        [mainTableView1.collectionView reloadData];
        
        
        [mainTableView1.collectionView headerEndRefreshing];
    }];
    
    
    
    
    
    
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.minimumInteritemSpacing = 10;
    flowlayout.minimumLineSpacing = 10;
    flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowlayout.itemSize = CGSizeMake(kWidth / 2 - 20, 0.4 * kHeight);
    
    
    self.collectionView.collectionViewLayout = flowlayout;
    
    
    
    [self.collectionView registerClass:[ChooseGiftCollectionViewCell class] forCellWithReuseIdentifier:@"HP"];
    [self.view addSubview:self.collectionView];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ChooseGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HP" forIndexPath:indexPath];
    ChooseGift *choose = self.modelArray[indexPath.row];
    cell.choose = self.modelArray[indexPath.row];
    cell.choose = choose;
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WebViewController *webvc = [[WebViewController alloc]init];
    webvc.userid = self.idArray[indexPath.row];
    webvc.choosegift = self.modelArray[indexPath.row];
    [webvc setHidesBottomBarWhenPushed:YES];
    webvc.type = 1;
    
    [self.navigationController pushViewController:webvc animated:YES];
    
    
    
    
}
#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
